package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.commom.R;
import com.itheima.reggie.entity.User;
import com.itheima.reggie.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Map;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;


    @PostMapping("/sendMsg")
    public R<String> sendMsg(@RequestBody User user, HttpSession session){
        log.info("接收到发送的请求的短信信息,phone={}", user.getPhone());
        String code = "123456";
        log.info("生成验证码:{}", code);
        session.setAttribute(user.getPhone(), code);
        return R.success("发送短信成功!");
    }
    @PostMapping("/login")
    public R<User> login(@RequestBody Map<String,String> map,HttpSession session){
        log.info("接收到前端登录请求:map={}", map);
        String phone = map.get("phone");//前端传的手机号
        String code = map.get("code");  //前端传的验证码
        String codeInSession = (String) session.getAttribute(phone); //session中的验证码
        if (codeInSession == null || !codeInSession.equals(code)) {
            //如果session中的验证码不存在，或者跟前端传递过来的验证码不一致
            log.warn("验证码错误 phone={}, code={}, codeInSession={}", phone, code, codeInSession);
            return R.error("验证码错误，请重试");
        }

        //2. 如果验证码一致
        //判断user中是否存在该手机号，
        //select * from user where phone = 13612345678;
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.eq(User::getPhone, phone);
        User user = userService.getOne(lqw);
        if (user == null) {
            //如果不存在，将手机号保存到user表（注册）
            user = new User();
            user.setPhone(phone);
            user.setStatus(1);
            //注册新用户
            userService.save(user);
        }
        //3.  将用户的id存入session
        session.setAttribute("user", user.getId());

        //4. 将session中的验证码移除掉
        session.removeAttribute(phone);
        return R.success(user);






   /*     //1.校验验证码是否与session中的验证码一致
         if(codeInSession!=null&&codeInSession.equals(code)){
             //如果比对成功,说明登录成功
             LambdaQueryWrapper<User> lqw=new LambdaQueryWrapper<>();
             lqw.eq(User::getPhone,phone);
             //查询用户
             User user = userService.getOne(lqw);
             if(user==null){
                 //如果没有,注册
                 user= new User();
                 user.setPhone(phone);
                 user.setStatus(1);
                 userService.save(user);
             }
             //3.将用户的id存入session中
             session.setAttribute("user",user.getId());
             //4.将session的验证码移除
             return  R.success(user);

         }
        return R.error("登录失败!");*/
    }

}